<?php
/**
 * Nordic Repository
 * 
 * LICENSE
 * 
 * The new BSD license is applied on this source-file. For further
 * information please visit http://license.nordic-dev.de/newbsd.txt
 * or send an email to andre.moelle@gmail.com.
 */

/**
 * Load necessary classes.
 */
Zend_Loader::loadClass('Zend_Controller_Action_Helper_Abstract');

Zend_Loader::loadClass('Nordic_Schema');

/**
 * This class encapsulates the loading of the translations.
 * 
 * @category   Nordic
 * @package    Nordic_Controller
 * @copyright  2007 Nordic Development
 * @license    http://license.nordic-dev.de/newbsd.txt (New-BSD license)
 * @author     Andre Moelle <andre.moelle@gmail.com>
 * @version    $Id: Translator.php 3 2007-07-08 09:21:42Z andre.moelle $
 */
class Nordic_Controller_Helper_Translator extends Zend_Controller_Action_Helper_Abstract
{
	/**
	 * Instance of a translator.
	 * 
	 * @var Zend_Translate
	 */
	private $_translator = null;
	
	/**
	 * Attaches one or more new translation-files.
	 * 
	 * <code>
	 * $file = 'foo';
	 * # would load: /lang/Controller/de.foo.xml
	 * </code>
	 * 
	 * @param string $file optional filename
	 * @return integer
	 */
	public function load ($file = '*')
	{
		$lang = Zend_Registry::get('Application')->language;
		
		$path = NORDIC_LANG . 
				$this->getActionController()->getControllerName() . 
				DIRECTORY_SEPARATOR . $lang . '.' . $file . '.*';
		
		$array = glob($path);
		
		if(!$array)
		{
			return 0;
		}
		
		$translator = Zend_Registry::get('Translator');
		$result = 0;
		
		foreach($array as $path)
		{
			$translator->addTranslation(
				Nordic_Schema::encodeFile($path),
				$lang
			);
			
			$result++;
		}
		
		return $result;
	}
}
?>